package com.jw.blog.wblog.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jw.blog.annotation.AccessLimit;
import com.jw.blog.wblog.entity.Visit;
import com.jw.blog.wblog.entity.custom.ResultBean;
import com.jw.blog.wblog.entity.custom.TimeAndCount;
import com.jw.blog.wblog.service.IVisitService;
import org.apache.catalina.LifecycleState;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Struct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wan Email-wlf18726994755@163.com
 * @version 1.0
 * @className VisitControllerAdmin
 * @date 2020/6/21 15:08
 **/

@RestController
public class VisitControllerAdmin {

    @Resource
    private IVisitService iVisitService;

    @GetMapping("/admin/selectVisitListByDate")
    public ResultBean<List<TimeAndCount>> selectVisitListByDate(@RequestParam(value = "format") String format,
                                                               @RequestParam(value = "startTime") String startTime,
                                                               @RequestParam(value = "endTime") String endTime){
        Map<String, Object> map = new HashMap<String, Object>();
        if (!StrUtil.hasEmpty(format)) {
            map.put("format", format);
        }
        if (!StrUtil.hasEmpty(startTime)) {
            map.put("startTime", startTime);
        }
        if (!StrUtil.hasEmpty(endTime)) {
            map.put("endTime", endTime);
        }
        return new ResultBean<>(iVisitService.selectVisitListByDate(map));
    }

    @PostMapping("/admin/selectVisitListByIp")
    public Map<String,Object> selectVisitListByIp(){
        QueryWrapper<Visit> wrapper = new QueryWrapper<>();
        wrapper.lambda().groupBy(Visit::getIp, Visit::getUserAgent).select(Visit::getIp,Visit::getUserAgent);
        Page<Visit> visitPage = new Page<>();
        Page<Visit> page = iVisitService.page(visitPage, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("user",page.getTotal());
        int count = iVisitService.count();
        map.put("count",count);
        return map;
    }

    @PostMapping("/admin/selectLikeVisitListByPage")
    public ResultBean<IPage<Visit>> selectLikeVisitListByPage(
            Visit visit,
            @RequestParam(value = "startTime") String startTime,
            @RequestParam(value = "endTime") String endTime,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "9") Integer pageSize
    ){
        QueryWrapper<Visit> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasEmpty(startTime)) {
            wrapper.lambda().ge(Visit::getTime,startTime);
        }
        if (!StrUtil.hasEmpty(endTime)) {
            wrapper.lambda().le(Visit::getTime,endTime);
        }
        if (!StrUtil.hasEmpty(visit.getIp())) {
            wrapper.lambda().like(Visit::getIp,visit.getIp()).or();
        }
        if (!StrUtil.hasEmpty(visit.getCity())) {
            wrapper.lambda().like(Visit::getCity,visit.getCity()).or();
        }
        if (!StrUtil.hasEmpty(visit.getPlatformType())) {
            wrapper.lambda().like(Visit::getPlatformType,visit.getPlatformType()).or();
        }
        if (!StrUtil.hasEmpty(visit.getBrowserType())) {
            wrapper.lambda().like(Visit::getBrowserType,visit.getBrowserType());
        }
       wrapper.lambda().orderByDesc(Visit::getTime);
        //分页查询
        Page<Visit> visitPage = new Page<>();
        visitPage.setCurrent(page);
        visitPage.setSize(pageSize);
         return new ResultBean<>( iVisitService.page(visitPage, wrapper));
    }


    /**
     * 模糊组合分页查询访客信息
     *
     * @param id，resource
     * @return
     * @throws Exception
     */
    /**
     * 模糊组合分页查询访客信息
     * @param visit 访客信息
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 当前页
     * @param pageSize 一页几条数据
     * @return 分页数据
     */
    @PostMapping("/admin/selectLikeVisitListGroupByIp")
    public  Map<String, Object> selectLikeVisitListGroupByIp(Visit visit,
                                                            @RequestParam(value = "startTime") String startTime,
                                                            @RequestParam(value = "endTime") String endTime,
                                                            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
                                                            @RequestParam(value = "pageSize", required = true, defaultValue = "9") Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StrUtil.isNotBlank(startTime)) {
            map.put("startTime", startTime);
        }
        if (StrUtil.isNotBlank(endTime)) {
            map.put("endTime", endTime);
        }
        if (StrUtil.isNotBlank(visit.getIp())) {
            map.put("ip", visit.getIp());
        }
        if (StrUtil.isNotBlank(visit.getCity())) {
            map.put("city", visit.getCity());
        }
        if (StrUtil.isNotBlank(visit.getPlatformType())) {
            map.put("platformType", visit.getPlatformType());
        }
        if (StrUtil.isNotBlank(visit.getBrowserType())) {
            map.put("browserType", visit.getBrowserType());
        }
        // 分页显示：第1页开始，每页显示9条记录
        PageHelper.startPage(page, pageSize);
        List<?> visitList = iVisitService.selectLikeVisitListGroupByIp(map);
        PageInfo pageInfo = new PageInfo(visitList);
        Map<String, Object> returnMap = new HashMap<>();
        if (visitList.size() > 0) {
            returnMap.put("status", 200);
        } else {
            // 500表示：返回值为Null
            returnMap.put("status", 500);
        }
        returnMap.put("visitList", visitList);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }

}
